/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "script.h"

namespace sleepyface
{
    namespace visual
    {
        namespace script
        {
            Environment::Environment()
            {
            }

            Color Environment::getColor(string c)
            {
                if (theme.find(c) == theme.end())
                    return Color();
                return theme[c];
            }

            Color Environment::setColor(string c, Color v)
            {
                return (theme[c] = v);
            }

            RenderCommand::RenderCommand()
            {
                cmd = pass;
            }

            RenderCommand::RenderCommand(string s)
            {
                cmd = pass;
                surface = s;
            }

            RenderCommand RenderCommand::createPass()
            {
                cmd = pass;
                return *this;
            }

            RenderCommand RenderCommand::createRectangle(Region r, Color c)
            {
                cmd = rectangle;
                region = r;
                color = c;
                return *this;
            }

            RenderCommand RenderCommand::createLine(Region r, Color c)
            {
                cmd = line;
                region = r;
                color = c;
                return *this;
            }

            RenderCommand RenderCommand::createText(Region r, Color c, string f, string t)
            {
                cmd = text;
                region = r;
                color = c;
                font = f;
                textData = t;
                return *this;
            }

            RenderCommand RenderCommand::createImage(Region r, Color c, string p)
            {
                cmd = image;
                region = r;
                color = c;
                imageData = p;
                return *this;
            }

            int RenderCommand::getCommand()
            {
                return cmd;
            }

            string RenderCommand::getSurface()
            {
                return surface;
            }

            Color RenderCommand::getColor()
            {
                return color;
            }

            string RenderCommand::getFont()
            {
                return font;
            }

            string RenderCommand::getText()
            {
                return textData;
            }

            string RenderCommand::getImage()
            {
                return imageData;
            }

            Region RenderCommand::getRegion()
            {
                return region;
            }

            Region RenderCommand::render(Renderer &renderer)
            {
                if (cmd == pass)
                {
                    return Region();
                }
                else if (cmd == rectangle)
                {
                    renderer.getSurface(surface).rect(region, color);
                    return Region();
                }
                else if (cmd == line)
                {
                    renderer.getSurface(surface).line(region, color);
                    return Region();
                }
                else if (cmd == text)
                {
                    renderer.getSurface(surface).text(renderer, region, color, font, textData);
                    return Region();
                }
                else if (cmd == image)
                {
                    Region rtn = Region();
                    renderer.loadImage(imageData, rtn);
                    renderer.getSurface(imageData).renderTo(renderer.getSurface(surface), rtn, region);
                    return rtn;
                }
                else
                {
                    return Region();
                }
            }

            string RenderCommand::display()
            {
                stringstream ss;
                
                if (cmd == pass)
                    ss << "pass";
                else if (cmd == rectangle)
                    ss << "rectangle";
                else if (cmd == line)
                    ss << "line";
                else if (cmd == text)
                    ss << "text";
                else if (cmd == image)
                    ss << "image";
                else
                    ss << "<unknown command " << cmd << ">";

                ss << " surface=" << surface << " color=" << color.display() << " region=" << region.display();

                if (!font.empty())
                    ss << " font=" << font;
                if (!textData.empty())
                    ss << " text=\"" << text << "\"";
                if (!imageData.empty())
                    ss << " image=" << imageData;

                return ss.str();
            }
        }
    }
}
